﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using CefSharp;
using DataAnalyse;

namespace DataAnalyseClient
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        RestUtils restUtils;
        public LoginWindow()
        {
            InitializeComponent();
            restUtils = AppStatic.restUtils;
            this.Loaded += LoginWindow_Loaded;
        }
        private void LoginWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //restUtils = new RestUtils();
            this.savePwd.IsChecked = AppStatic.local.isSave;
            this.isAuto.IsChecked = AppStatic.local.autoLogin;
            if (AppStatic.local.isSave)
            {
                this.accountTxt.Text = AppStatic.local.account;
                this.passwdTxt.Password = AppStatic.local.passwd;
            }
            if(AppStatic.local.autoLogin&&!AppStatic.isLoginOut)
            {
                if(!string.IsNullOrEmpty(AppStatic.local.account)&&!string.IsNullOrEmpty(AppStatic.local.passwd))
                {
                    Action action = () =>
                    {
                        if (restUtils.login(AppStatic.local.account, AppStatic.local.passwd))
                        {
                            this.DialogResult = true;
                            this.Close();
                        }
                    };
                    this.Dispatcher.BeginInvoke(action);
                } 
                else
                {
                    MessageBox.Show("账号或密码为空");
                }
            }   
        }

        private void savePwd_Checked(object sender, RoutedEventArgs e)
        {
            AppStatic.local.isSave = true;
        }

        private void savePwd_Unchecked(object sender, RoutedEventArgs e)
        {
            AppStatic.local.isSave = false;
        }

        private void isAuto_Checked(object sender, RoutedEventArgs e)
        {
            AppStatic.local.isSave = true;
            AppStatic.local.autoLogin = true;
            this.savePwd.IsChecked = true;
        }

        private void isAuto_Unchecked(object sender, RoutedEventArgs e)
        {
            AppStatic.local.autoLogin = false;
        }

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (!string.IsNullOrEmpty(this.accountTxt.Text)&&!string.IsNullOrEmpty(this.passwdTxt.Password))
            {
                Action action = () =>
                {
                    if (restUtils.login(this.accountTxt.Text, this.passwdTxt.Password))
                    {
                        AppStatic.doAfterLogin(this.accountTxt.Text, this.passwdTxt.Password);

                        if(AppStatic.needRegister)
                        {
                            DeviceAuthorizeWindow window = new DeviceAuthorizeWindow();
                            var msgResult = window.ShowDialog();
                            if (msgResult.Value)
                            {
                                if(restUtils.requestAuthorize(AppStatic.deviceCode))
                                {
                                    this.DialogResult = true;
                                    this.Close();
                                }
                            }
                            else
                            {
                                this.DialogResult = false;
                                this.Close();
                            }
                        }
                        else
                        {
                            this.DialogResult = true;
                            this.Close();
                        }
                        
                    }
                };
                this.Dispatcher.BeginInvoke(action);
            } 
            else
            {
                MessageBox.Show("账号或密码为空");
            }
        }

        private void Border_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
